<?php namespace Phpcmf\Controllers;

/**
 * 二次开发时可以修改本文件，不影响升级覆盖
 */

class Tieziliebiao extends \Phpcmf\Home\Module
{

	public function index() {
		// 获取get参数
		$catid = (int)\Phpcmf\Service::L('input')->get('catid');
		$pagesize = (int)\Phpcmf\Service::L('input')->get('pagesize'); // 页数量
		$page = (int)\Phpcmf\Service::L('input')->get('page'); // 当前页
		$shuxing = (int)\Phpcmf\Service::L('input')->get('shuxing'); // 属性 0是最新，1是最热，2热评
		$uid = (int)\Phpcmf\Service::L('input')->get('uid');
		$gongkai = (int)\Phpcmf\Service::L('input')->get('gongkai');
		$syxs = (int)\Phpcmf\Service::L('input')->get('syxs');
		$tuijian = (int)\Phpcmf\Service::L('input')->get('tuijian'); //首页推荐
		$jinghua = (int)\Phpcmf\Service::L('input')->get('jinghua'); //精华
		$rumen = (int)\Phpcmf\Service::L('input')->get('rumen'); //热门
		$reping = (int)\Phpcmf\Service::L('input')->get('reping'); //热评
		$guanzhu = (int)\Phpcmf\Service::L('input')->get('guanzhu'); //关注
		$shoucang = (int)\Phpcmf\Service::L('input')->get('shoucang'); //收藏
		$dianzan = (int)\Phpcmf\Service::L('input')->get('dianzan'); //点赞
		$pinglun = (int)\Phpcmf\Service::L('input')->get('pinglun'); //评论
		$lishi = (int)\Phpcmf\Service::L('input')->get('lishi'); //历史
		$search = \Phpcmf\Service::L('input')->get('search'); //搜索
		//图片属性
		$tupianzhi =  (int)\Phpcmf\Service::L('input')->get('tupianzhi'); //图片
		//视频属性
		$shipinzhi =  (int)\Phpcmf\Service::L('input')->get('shipinzhi'); //视频
		//音频属性
		$yinpinzhi =  (int)\Phpcmf\Service::L('input')->get('yinpinzhi'); //音频
		//附件属性
		$fujianzhi =  (int)\Phpcmf\Service::L('input')->get('fujianzhi'); //附件
		//付费内容
		$fufeizhi = (int)\Phpcmf\Service::L('input')->get('fufeizhi'); //付费内容
		//关注
		if($guanzhu){
		    
		    $guanzhu_data_list = \Phpcmf\Service::M()->table('member_guanzhu')->where('uid', $this->uid)->getAll();
		}
		//收藏
		if($shoucang) {
		    $shoucang_data_list = \Phpcmf\Service::M()->table('1_quan_form_quantie_shoucang')->where('uid', $this->uid)->getAll();
		}
		//点赞
		if($dianzan){
		    $dianzan_data_list = \Phpcmf\Service::M()->table('1_quan_form_quantie_dianzan')->where('uid', $this->uid)->getAll();
		}
		//评论
		if($pinglun){
		    $pinglun_data_list = \Phpcmf\Service::M()->table('1_quan_form_quantie_comment')->where('uid', $this->uid)->getAll();
		}
		//历史
		if($lishi){
		    $lishi_data_list = \Phpcmf\Service::M()->table('1_quan_form_quantie_lishi')->where('uid', $this->uid)->getAll();
		}
		if($shuxing == 1){
			$order = 'hits desc';
		} elseif($shuxing == 2){
			$order = 'comments desc';
		} else {
			$order = 'id desc,inputtime desc'; //, 'desc'
		}
		if(!$pagesize){
			$pagesize = 20; // 默认值
		}
		if(!$page){
			$page = 1; // 默认第一页
		}
		// 构建查询条件
		$query = \Phpcmf\Service::M()->db->table('1_quan_form_quantie as qf')
		    ->select('qf.*')
		    ->join('1_quan as q', 'qf.cid = q.id')
		    ->where('qf.status', 1);
		// 是否显示首页
	    if($syxs==1){
	        $query->where('q.syxs', 1);
	    }
		    
		//栏目id
		if($catid){
		    $query->where('qf.cid', $catid); //栏目id
		}
		//首页推荐
		if($tuijian){
		    $query->where('qf.qztj', $tuijian);
		}
		//精华
		if($jinghua){
		    $query->where('qf.jinghua', $jinghua);
		}
		//热门
		if($rumen){
		    $order = 'qf.hits desc';
		}
		//热评
		if($reping){
		    $order = 'qf.comments desc';
		    $query->where('qf.comments >', 0);
		}
		//关注
		if($guanzhu){
		    // uid 获取关注 的id
		    // 创建空数组存储所有tieid
		    $guanzhu_ids = [];
		    // 遍历获取所有的tieid
		    foreach ($guanzhu_data_list as $item) {
		        $guanzhu_ids[] = $item['touid'];
		    }
		    
		    // 如果有收藏的id，就添加到查询条件中
		    if (!empty($guanzhu_ids)) {
		        $query->where_in('qf.uid', $guanzhu_ids);
		    } else {
		        // 如果没有收藏记录，返回空结果
		        $query->where('qf.uid', 0);
		    }
		}
		//收藏
		if($shoucang){
		    // uid 获取收藏 的id
		    // 创建空数组存储所有tieid
		    $shoucang_ids = [];
		    // 遍历获取所有的tieid
		    foreach ($shoucang_data_list as $item) {
		        $shoucang_ids[] = $item['tieid'];
		    }
		    
		    // 如果有收藏的id，就添加到查询条件中
		    if (!empty($shoucang_ids)) {
		        $query->where_in('qf.id', $shoucang_ids);
		    } else {
		        // 如果没有收藏记录，返回空结果
		        $query->where('qf.id', 0);
		    }
		}
		//点赞
		if($dianzan){
		    // 创建空数组存储所有tieid
		    $dianzan_ids = [];
		    // 遍历获取所有的tieid
		    foreach ($dianzan_data_list as $item) {
		        $dianzan_ids[] = $item['tieid'];
		    }
		    
		    // 如果有的id，就添加到查询条件中
		    if (!empty($dianzan_ids)) {
		        $query->where_in('qf.id', $dianzan_ids);
		    } else {
		        // 如果没有收藏记录，返回空结果
		        $query->where('qf.id', 0);
		    }
		}
		//评论
		if($pinglun){
		    // 创建空数组存储所有tieid
		    $pinglun_ids = [];
		    // 遍历获取所有的tieid
		    foreach ($pinglun_data_list as $item) {
		        $pinglun_ids[] = $item['cid'];
		    }
		    
		    // 如果有的id，就添加到查询条件中
		    if (!empty($pinglun_ids)) {
		        $query->where_in('qf.id', $pinglun_ids);
		    } else {
		        // 如果没有收藏记录，返回空结果
		        $query->where('qf.id', 0);
		    }
		}
		//历史
		if($lishi){
		    // 创建空数组存储所有tieid
		    $lishi_ids = [];
		    // 遍历获取所有的tieid
		    foreach ($lishi_data_list as $item) {
		        $lishi_ids[] = $item['tieid'];
		    }
		    
		    // 如果有的id，就添加到查询条件中
		    if (!empty($lishi_ids)) {
		        $query->where_in('qf.id', $lishi_ids);
		    } else {
		        // 如果没有收藏记录，返回空结果
		        $query->where('qf.id', 0);
		    }
		}
		//图片值
        if($tupianzhi){
            //$query->where('qf.tpkg', 1);
        	// 对于jiugetu字段，我们需要使用where条件来筛选有file数据的记录
        	// jiugetu字段的数据结构是JSON格式: {"file":["106","107"],"title":["tupian","tupian"],"description":["",""]}
        	// 使用LIKE条件来确保file数组不为空
        	$query->where('qf.jiugetu LIKE', '%\"file\":[%');
        	$query->where('qf.jiugetu NOT LIKE', '%\"file\":[]%')->where('qf.tpkg', 1);
        }
        //音频值
        if($yinpinzhi){
        	$query->where('qf.yinpin LIKE', '%\"file\":[%');
        	$query->where('qf.yinpin NOT LIKE', '%\"file\":[]%')->where('qf.tpkg', 1);
        }
        //视频值
        if($shipinzhi){
            //$query->where('qf.tpkg', 1);
        	// 对于shipin字段，我们需要使用where条件来筛选有file数据的记录
        	// shipin字段的数据结构是JSON格式: {"file":["106","107"],"title":["tupian","tupian"],"description":["",""]}
        	// 使用LIKE条件来确保file数组不为空
        	$query->where('qf.shipin LIKE', '%\"file\":[%');
        	$query->where('qf.shipin NOT LIKE', '%\"file\":[]%')->where('qf.spkg', 1);
        }
        //附件值
        if($fujianzhi){
        	$query->where('qf.wenjian LIKE', '%\"file\":[%');
        	$query->where('qf.wenjian NOT LIKE', '%\"file\":[]%')->where('qf.fjkg', 1);
        }
        //付费内容值
        if($fufeizhi){
        	$query->where('qf.fufeineirong <>', '');
        	$query->where('qf.nrkg', 1);
        }
		//用户id
		if($uid > 0){
			$query->where('qf.uid', $uid);
		}
		//公开展示
		if($gongkai ==1){
		    $query->where('qf.gongkai', 1);
		}
		//搜索
		if($search){
		    $query->where('qf.neirong LIKE', '%'.$search.'%');
		}
		
		$page_data = $page; // 从第一页开始
		$per_page = $pagesize; // 每页20条
		$offset = ($page_data - 1) * $per_page;
		//->get()->getResultArray();
		//$shuju_data = $query->order_by($order)->limit($per_page, $offset)->getAll();
		$shuju_data = $query->orderBy($order)->limit($per_page, $offset)->get()->getResultArray();
		//圈子设置
		$quanzhi_shezhi_0 = \Phpcmf\Service::M()->table('1_quan_shezhi')->where('dirname', 'quan')->getRow();
		$quanzhi_shezhi = dr_string2array($quanzhi_shezhi_0['setting']);
		// 循环 $shuju_data ， 把 $shuju_data['content'] 中的图片路径替换为 域名 + 图片路径
		$list_data = [];
		foreach ($shuju_data as $key => $val) {
		    $query_data = \Phpcmf\Service::M()->db->table('1_quan')->where('fstatus', 1);
		    if($syxs == 1){
		        $query_data->where('syxs', 1);
		    }
			$quan_data = $query_data->where('id', $val['cid'])->get()->getRowArray();
			
			// 内容图片列表
			$tupian_data = dr_get_content_img($val['neirong']);
			$tupian_list = [];
			foreach ($tupian_data as $k => $v) {
				// 去除&quot;及前面的内容
                $v = preg_replace('/.*&quot;/', '', $v);
				$tupian_list[] = dr_thumb($v, 250, 250, 0, 'crop');
				
			}
			// 图片容器
			
			// 头像
			if(dr_avatar($val['uid'])){
				$touxiang = dr_avatar($val['uid']);
			} else {
				$touxiang = 1;
			}
			// 九个图 
			$jiugetu_data = dr_string2array($val['jiugetu']);
			$jiugetu_list = [];
			foreach ($jiugetu_data['file'] as $k => $v) {
				if ($k <= 8) {
					$jiugetu_list[] = dr_thumb($v, 250, 250, 0, 'crop');
				}
			}
			//判断$tupian_list是否有图片
			if($tupian_list){
				$tupian = $tupian_list;
			} else {
				$tupian = $jiugetu_list;
			}
			// 音频 
			$yinpin_data = dr_string2array($val['yinpin']);
			$yinpin_list = [];
			foreach ($yinpin_data['file'] as $k => $v) {
				if ($k <= 8) {
					$yinpin_list[] = dr_get_file($v);
				}
			}
			// 视频
			$shipin_data = dr_string2array($val['shipin']);
			$shipin_list = [];
			// 支持多种视频格式转换为.webp
			$video_extensions = ['.mp4', '.mov', '.m4v', '.rm', '.rmvb', '.3gp', '.avi', '.flv', '.webm', '.mkv'];
			foreach ($shipin_data['file'] as $k => $v) {
			    $shipintu_list = str_ireplace($video_extensions, '', dr_get_file($v));
				if ($k <= 8) {
					$shipin_list[] = [
					    'shipin' => dr_get_file($v),
					    'shipintu' => $shipintu_list,
					    ];
				}
			}

			// 名称
			$userweixin = \Phpcmf\Service::M()->db->table('member_oauth')->where('uid', $val['uid'])->get()->getRowArray();
			$user = dr_member_info($val['uid']);
			if($userweixin){
				$mingcheng = $userweixin['nickname'];
			} else if($user['name']) {
				$mingcheng = $user['name'];
			} else {
				$mingcheng = $user['username'];
			}
			$dianzan = \Phpcmf\Service::M()->db->table('1_quan_form_quantie_dianzan')->where('tieid', $val['id'])->countAllResults();
			$shoucang = \Phpcmf\Service::M()->db->table('1_quan_form_quantie_shoucang')->where('tieid', $val['id'])->countAllResults();
			//$pinglun = \Phpcmf\Service::M()->db->table('1_quan_form_quantie_comment')->where('cid', $val['id'])->where(' status', 1)->countAllResults();
			// 点赞用户头像
			$dianzan_data = \Phpcmf\Service::M()->table('1_quan_form_quantie_dianzan')->where('tieid', $val['id'])->getAll();
			$dianzan_touxiang = [];
			foreach ($dianzan_data as $k => $v) {
				$dianzan_touxiang[] = dx_touxiang($v['uid']); //dr_avatar($v['uid'])
			}
			// 进度条
			$jindu_data_0 = \Phpcmf\Service::M()->db->table('1_quan_form_jindu_index')->where('tieid', $val['id'])->where('quanid', $val['cid'])->get()->getRowArray();
			if($jindu_data_0){
				$jindu = [
				    'tieid' => $val['id'],
					'jindu' => 1,
					'title' => $jindu_data_0['title'],
					'beizhu' => $jindu_data_0['beizhu'],
					'zhuangtai' => $jindu_data_0['zhuangtai'],
					'tianjiashijian' => dr_date($jindu_data_0['tianjiashijian'], 'm-d-H:i'),
					'kaishishijian' => dr_date($jindu_data_0['kaishishijian'], 'm-d-H:i'),
					'jieshushijian' => dr_date($jindu_data_0['jieshushijian'], 'm-d-H:i'),
				];

			} else {
				$jindu = [
					'jindu' => 0,
				];
			}
			
			    // $quan_data_id = \Phpcmf\Service::M()->db->table('1_quan')->where('id', $val['cid'])->get()->getRowArray();
				// 帖子内容列表
				$list_data[] = [
					'id' => $val['id'],
					'cid' => $val['cid'],
					'uid' => $val['uid'],
					'touxiang' => $touxiang,
					'mingcheng' => $mingcheng,
					'title' => $val['title'],
					'tupian' => $tupian,
					//'jiugetu' => $tupian,
					'jianjie' => dr_strcut(dr_clearhtml($val['neirong']), 100),
					'fabushijian' => dr_date($val['inputtime'], 'Y-m-d H:i:s'),
					'quanzi' => $quan_data['title'],
					'dianzan' => $dianzan,
					'pinglun' => $val['comments'],
					'shoucang' => $shoucang,
					'dianzan_touxiang' => $dianzan_touxiang,
					'jindu' => $jindu,
					'tuijian' => $val['tuijian'], //首页推荐
					'quanzhantuijian' => $val['qztj'], //全站推荐
					'quanneituijian' => $val['qntj'], //圈内推荐
					'jinghua' => $val['jinghua'], //精华
					'shipin' => $shipin_list,
					'shipin_kaiguan' => $val['spkg'], //视频开关
					'yinpin' => $yinpin_list, //音频数据
					'quan_tubiao' => dr_get_file($quan_data['thumb']),
					'quan_id' => $quan_data['id'],
					'quan_ming' => $quan_data['title'],
					'quan_jiashao' => $quan_data['description'],
					'quan_touxiang' => $quanzhi_shezhi['pages']['quan_touxiang'],
					//'uid_data' => $this->uid,
					//'shoucangid' => $shoucang_ids,
				];
			if($quan_data){}
			
		}

		$this->_json('1', '读取成功', $list_data);
	}

}
